/**
 * 
 */
package com.urg.parser;

/**
 * @author UrG
 * 
 */
public class Condition {
	/** */
	private String conditionString;
	/** */
	private Expression expression1;
	/** */
	private boolean hasSecondCondition;
	/** */
	private ConditionSeparator conditionSeparator;
	/** */
	private Condition condition;

	/**
	 * @param condition
	 */
	public Condition(String condition) {
		this.conditionString = condition;
		parseCondition(this.conditionString);
	}

	/**
	 * Parses a given condition.
	 * 
	 * @param condition2
	 */
	public void parseCondition(String condition) {
		condition = BracketAnalyzer.smartlyTrimEndBrackets(condition);
		if (condition.startsWith("(")) {
			this.hasSecondCondition = true;
		} else {
			this.hasSecondCondition = false;
		}
		if (this.hasSecondCondition) {
			int endBracketIndex = BracketAnalyzer.getEndBracketIndex(condition);
			String expression1 = condition.substring(
					BracketAnalyzer.getStartBracketIndex(condition) + 1,
					endBracketIndex);
			this.expression1 = new Expression(expression1.trim());
			String restCondition = condition.substring(endBracketIndex + 1)
					.trim();
			String conditionSeparator = restCondition.substring(0,
					restCondition.indexOf("(")).trim();
			this.conditionSeparator = identifyConditionSeparator(conditionSeparator);
			this.condition = new Condition(
					restCondition.substring(restCondition.indexOf("(")));
		} else {
			this.expression1 = new Expression(condition);
		}
	}

	/**
	 * Identifies a condition separator.
	 * 
	 * @param conditionSeparator
	 * @return {@link ConditionSeparator}
	 */
	public static ConditionSeparator identifyConditionSeparator(
			String conditionSeparator) {
		ConditionSeparator separator = ConditionSeparator.AND;
		if ("AND".equalsIgnoreCase(conditionSeparator)
				|| "&&".equalsIgnoreCase(conditionSeparator)) {
			separator = ConditionSeparator.AND;
		}
		if ("OR".equalsIgnoreCase(conditionSeparator)
				|| "||".equalsIgnoreCase(conditionSeparator)) {
			separator = ConditionSeparator.OR;
		}
		return separator;
	}

	@Override
	public String toString() {
		String condition = "";
		if (hasSecondCondition) {
			condition += "((" + expression1 + ")";
			condition += " " + conditionSeparator + " "
					+ this.condition.toString() + ")";
		} else {
			condition += "(" + expression1 + ")";
		}
		return condition;
	}

	/**
	 * @return the conditionString
	 */
	public String getConditionString() {
		return conditionString;
	}

	/**
	 * @return the expression1
	 */
	public Expression getExpression1() {
		return expression1;
	}

	/**
	 * @return the hasSecondCondition
	 */
	public boolean isHasSecondCondition() {
		return hasSecondCondition;
	}

	/**
	 * @return the conditionSeparator
	 */
	public ConditionSeparator getConditionSeparator() {
		return conditionSeparator;
	}

	/**
	 * @return the condition
	 */
	public Condition getCondition() {
		return condition;
	}

}
